<?php


namespace App\Http\Controllers\Admin\Gift;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

//礼物标签
class TagController extends BaseController
{
    /**
     * @api {post} /gift/tag 新增礼物标签
     *
     * @apiGroup 礼物-标签
     *
     * @apiParam {string} tag_name 标签名称，必须
     * @apiParam {string} bg_color 背景颜色，示例：#0000FF，必须
     * @apiParam {string} status 状态：1：启用；0：禁用，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function add(Request $request)
    {
        $param = $request->only('tag_name', 'bg_color', 'status');
        $v = app('validator')->make($param, [
            'tag_name' => 'required|string|max:4',
            'bg_color' => 'required|string',
            'status' => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        if (app('repo_gift_tag')->addOrEdit($param)) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '新增失败');
    }

    /**
     * @api {put} /gift/tag 编辑礼物标签
     *
     * @apiGroup 礼物-标签
     *
     * @apiParam {Int} id 要编辑的记录id，必须
     * @apiParam {string} tag_name 标签名称，必须
     * @apiParam {string} bg_color 背景颜色，示例：#0000FF，必须
     * @apiParam {string} status 状态：1：启用；0：禁用，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function edit(Request $request)
    {
        $param = $request->only('id', 'tag_name', 'bg_color', 'status');
        $v = app('validator')->make($param, [
            'id' => 'required|integer',
            'tag_name' => 'required|string|max:4',
            'bg_color' => 'required|string',
            'status' => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        if (app('repo_gift_tag')->addOrEdit($param)) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '编辑失败');
    }

    /**
     * @api {delete} /gift/tag 删除礼物标签
     *
     * @apiGroup 礼物-标签
     *
     * @apiParam {string} ids 要编辑的记录id，多个用英文逗号连接，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function del(Request $request)
    {
        $ids = $request->ids;

        if (empty($ids)) throw new BasicException(0, '请选择要删除的标签');

        $ids = explode(',', $ids);

        DB::beginTransaction();
        try {
            app('model_gift_tag')->whereIn('id', $ids)->delete();
            app('model_gift_tag_join')->whereIn('tag_id', $ids)->delete();

            DB::commit();
            return Response::sendData([]);
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(0, $e->getMessage());
        }
    }

    /**
     * @api {get} /gift/tag 礼物标签列表
     *
     * @apiGroup 礼物-标签
     *
     * @apiParam {string} tag_name 标签名称
     * @apiParam {int} status 状态：-1（默认）：不限制；1：启用；0：禁用
     * @apiParam {int} page 第几页
     * @apiParam {int} page_size 每页显示条数，默认10
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "id": 1, 序号
    "name": "热销", 名称
    "bg_color": "#000000", 背景颜色
    "status": 1, 状态：1：启用；0：禁用
    "created_at": "2021-08-11 15:32:27" 创建时间
    }
    ],
    "first_page_url": "http://local.qipaoadmin/gift/tag?page=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "http://local.qipaoadmin/gift/tag?page=1",
    "next_page_url": null,
    "path": "http://local.qipaoadmin/gift/tag",
    "per_page": 10,
    "prev_page_url": null,
    "to": 1,
    "total": 1
    }
    }
     */
    public function list(Request $request)
    {
        $param = $request->only('tag_name', 'status', 'page', 'page_size');
        $v = app('validator')->make($param, [
            'tag_name' => 'string|max:4',
            'status' => 'integer|in:-1,0,1',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $tagName = $param['tag_name'] ?? '';
        $status = $param['status'] ?? -1;
        $pageSize = $param['page_size'] ?? 10;

        $where = [];
        if ($tagName) {
            $where[] = ['name', 'like', "%$tagName%"];
        }
        if ($status > -1) {
            $where[] = ['status', '=', $status];
        }

        $list = app('model_gift_tag')
            ->select('id', 'name', 'bg_color', 'status', 'created_at')
            ->where($where)
            ->paginate($pageSize);
        return Response::sendData($list);
    }

    /**
     * @api {get} /gift/tag/all 获取所有礼物标签
     *
     * @apiGroup 礼物-标签
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": [
    {
    "id": 1, 序号
    "name": "热销" 名称
    }
    ]
    }
     */
    public function all()
    {
        $list = app('model_gift_tag')
            ->select('id', 'name')
            ->where('status', 1)
            ->get();
        return Response::sendData($list);
    }
}